<?php
/**
 * Created by PhpStorm.
 * User: zing
 * Date: 2018/5/4
 * Time: 10:26
 */

/**
 * 浏览器友好的变量输出
 * @param mixed $var 变量
 * @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
 * @param string $label 标签 默认为空
 * @param boolean $strict 是否严谨 默认为true
 * @return void|string
 */
function dump($var, $echo=true, $label=null, $strict=true) {
    $label = ($label === null) ? '' : rtrim($label) . ' ';
    if (!$strict) {
        if (ini_get('html_errors')) {
            $output = print_r($var, true);
            $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
        } else {
            $output = $label . print_r($var, true);
        }
    } else {
        ob_start();
        var_dump($var);
        $output = ob_get_clean();
        if (!extension_loaded('xdebug')) {
            $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
            $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
        }
    }
    if ($echo) {
        echo($output);
        return null;
    }else
        return $output;
}

class T
{
    public $big_pt_id;//优惠id
    public $rule_id;//规则id

    public $buy_tickets;//购买的票

    public $rules;

    public $pt_tickets;

    public $ticket_num;//重新组织的票 supplier_ticket_id=>buy_num

    public $total_price = 0.00;//总价
    public $real_price = 0.00;//优惠后实际价格
    public $disc_price = 0.00;//优惠价格


    public function __construct()
    {
//***************************************************测试数据***************************************************/
        $this->pt_tickets = [
            ["1", "2", "3"],//源
            ["4", "5", "10", "20"]//目标
        ];

        /**
         * 订单票信息(biz_order_ticket) 可售门票信息表(biz_ticket)
         * total_price 9100
         */

        //1100 + 150 + 200 = 1450
        $this->buy_tickets = [
            //普通票
            'common' => [//1100
                ['supplier_ticket_id' => 1, 'biz_ticket_id' => 10, 'buy_num' => 10, 'ticket_sale_price' => 10],//100
                ['supplier_ticket_id' => 2, 'biz_ticket_id' => 20, 'buy_num' => 10, 'ticket_sale_price' => 10],//100
                ['supplier_ticket_id' => 3, 'biz_ticket_id' => 30, 'buy_num' => 10, 'ticket_sale_price' => 10],//100
                ['supplier_ticket_id' => 4, 'biz_ticket_id' => 40, 'buy_num' => 20, 'ticket_sale_price' => 20],//400
                ['supplier_ticket_id' => 5, 'biz_ticket_id' => 50, 'buy_num' => 20, 'ticket_sale_price' => 20],//400
            ],
            //座位票
            'site' => [//150
                ['supplier_ticket_id' => 10, 'biz_ticket_id' => 100, 'buy_num' => 1, 'ticket_sale_price' => 10],
                ['supplier_ticket_id' => 10, 'biz_ticket_id' => 101, 'buy_num' => 1, 'ticket_sale_price' => 10],
                ['supplier_ticket_id' => 10, 'biz_ticket_id' => 102, 'buy_num' => 1, 'ticket_sale_price' => 10],
                ['supplier_ticket_id' => 10, 'biz_ticket_id' => 103, 'buy_num' => 1, 'ticket_sale_price' => 10],
                ['supplier_ticket_id' => 10, 'biz_ticket_id' => 104, 'buy_num' => 1, 'ticket_sale_price' => 10],//50
                ['supplier_ticket_id' => 11, 'biz_ticket_id' => 110, 'buy_num' => 1, 'ticket_sale_price' => 20],
                ['supplier_ticket_id' => 11, 'biz_ticket_id' => 111, 'buy_num' => 1, 'ticket_sale_price' => 20],
                ['supplier_ticket_id' => 11, 'biz_ticket_id' => 112, 'buy_num' => 1, 'ticket_sale_price' => 20],
                ['supplier_ticket_id' => 11, 'biz_ticket_id' => 113, 'buy_num' => 1, 'ticket_sale_price' => 20],
                ['supplier_ticket_id' => 11, 'biz_ticket_id' => 114, 'buy_num' => 1, 'ticket_sale_price' => 20],//100
            ],
            //酒店票
            'hotel' => [//200
                ['supplier_ticket_id' => 20, 'biz_ticket_id' => 0, 'buy_num' => 10, 'ticket_sale_price' => 10],
                ['supplier_ticket_id' => 21, 'biz_ticket_id' => 0, 'buy_num' => 10, 'ticket_sale_price' => 10]
            ]
        ];
        $this->rules = [
            "rule_id" => "4",
            "bottom_price" => "1000.00",
            "peak_price" => "2000.00",
            "num" => "1",
            "discount" => "0.8"
        ];
        //***************************************************测试数据***************************************************/
    }

    public function calcDiscount()
    {
        //计算所有票的总价格
        foreach ($this->buy_tickets as $buy_ticket) {
            foreach ($buy_ticket as $value) {
                $this->total_price += $value['ticket_sale_price'] * $value['buy_num'];
            }
        }
        echo "总价 total_price -> " . $this->total_price . "<br>";

        //判断是否属于优惠分类
        $classTickets = $this->classifyBuyTickets();
        dump($classTickets['source']);
        dump($classTickets['target']);
        dump($classTickets['other']);
    }

    /**
     * 将购买的票分类返回
     * @return array
     */
    public function classifyBuyTickets()
    {
        $ticket_ids = [];
        foreach ($this->buy_tickets as $ticket) {
            $ticket_ids = array_merge($ticket_ids, array_keys(array_flip(array_column($ticket, 'supplier_ticket_id'))));
        }
        $pt_tickets = $this->getPt();
        $source_tickets = $pt_tickets[0];
        $target_tickets = $pt_tickets[1];
        $classify_source_tickets = array_merge(array_intersect($ticket_ids, $source_tickets));
        $classify_target_tickets = array_merge(array_intersect($ticket_ids, $target_tickets));
        $classify_other_tickets = array_merge(array_diff($ticket_ids, array_merge($classify_source_tickets, $classify_target_tickets)));
        return ['source' => $classify_source_tickets, 'target' => $classify_target_tickets, 'other' => $classify_other_tickets];
    }
    /**
     * 获取优惠信息
     * @return mixed
     */
    public function getPt()
    {
        return $this->pt_tickets;
    }
}

$main = new T();
$main->calcDiscount();